//
//  UserViewController.m
//  ManStoreDemo
//
//  Created by qianfeng01 on 15/7/17.
//  Copyright (c) 2015年 xiangmu. All rights reserved.
//

#import "UserViewController.h"
#import "SDImageCache.h"
#import "MyFavoriteViewController.h"
#import "UMSocial.h"
@interface UserViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UMSocialUIDelegate>

{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
}
@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *dataArray;
@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray=[[NSMutableArray alloc] initWithObjects:@"联系我们:1791999589@qq.com",@"清除缓存",@"我的收藏",@"分享好友", nil];
    [self createTableView];
    
}
-(void)createTableView{
    _tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    [self.view addSubview:_tableView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID=@"cellID";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell==nil) {
        cell=[[UITableViewCell alloc] init];
        
    }
    cell.textLabel.text=self.dataArray[indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        
        case 1:
        {
            UIActionSheet *sheet=[[UIActionSheet alloc] initWithTitle:[NSString stringWithFormat:@"删除缓存文件:%.6fM",[self getCachesSize]] delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:nil];
            [sheet showInView:self.view];
        }
            break;
        case 2:
        {
            MyFavoriteViewController *controller=[[MyFavoriteViewController alloc] init];
            [self.navigationController pushViewController:controller animated:YES];
        }
            break;
        case 3:
        {
            NSString *text = [NSString stringWithFormat:@"软件网址"];
            
            [UMSocialSnsService presentSnsIconSheetView:self appKey:@"55b496cc67e58e46e70088a5"
                                              shareText:text
                                             shareImage:[UIImage imageNamed: @"account_candou"]
                                        shareToSnsNames:@[UMShareToSina,UMShareToSms,UMShareToEmail,UMShareToRenren]
                                               delegate:self];
            
        }
            break;

        default:
            break;
    }
}
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
    
}
-(double)getCachesSize{
    double sdSize=[[SDImageCache sharedImageCache] getSize];
    NSString *myCachePath=[NSHomeDirectory() stringByAppendingFormat:@"/Library/Caches/MyCaches"];
    NSDirectoryEnumerator *enumerator=[[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
    double mySize;
    for (NSString *fileName in enumerator) {
        NSString *filePath=[myCachePath stringByAppendingPathComponent:fileName];
        NSDictionary *dict=[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        mySize +=dict.fileSize;
    }
    double totalSize=(mySize+sdSize)/1024/1024;//转化为M
    return totalSize;
    
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==actionSheet.destructiveButtonIndex) {
        [[SDImageCache sharedImageCache] clearMemory];
        [[SDImageCache sharedImageCache] clearDisk];
        NSString *myCachePath=[NSHomeDirectory() stringByAppendingFormat:@"/Library/Caches/MyCaches"];
        [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
        
    }
}
-(BOOL)isDirectShareInIconActionSheet
{
    return YES;
}


@end
